package com.coms514.scheduleme;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.util.Log;

import com.google.api.services.calendar.model.Calendar;
import com.google.api.services.calendar.model.CalendarListEntry;
import com.google.api.services.calendar.model.Event;
import com.google.api.services.calendar.model.Events;

public class EventModel {
	// map a EventInfo to a Event Summary which is generated by Google Calendar
	 private final Map<String, EventInfo> events = new HashMap<String, EventInfo>();
	 com.google.api.services.calendar.Calendar client;
	 String calendarId;

	 EventModel (com.google.api.services.calendar.Calendar client, String calendarId) {
		 this.client = client;
		 this.calendarId = calendarId;
	 }
	 
	 private String key(EventInfo eventInfo) {
		 return eventInfo.id; // the google event Id is the key fro Map data structure
	 }
	 
	 private String key(Event event) {
		 return event.getId();
	 }
	 
	  int size() {
	    synchronized (events) {
	      return events.size();
	    }
	  }

	  void remove(EventInfo eventInfo) {
	    synchronized (events) {
	      events.remove(key(eventInfo));
	    }
	  }

	  private EventInfo get(String key) {
	    synchronized (events) {
	      return events.get(key);
	    }
	  }

	  // Event is type defined by google Calendar API
	  void add(Event eventToAdd) {
	    synchronized (events) {
	      EventInfo found = get(key(eventToAdd));
	      if (found == null) { // can't find the event in the map
	        events.put(key(eventToAdd), new EventInfo(eventToAdd));
	      } else {
	        found.update(eventToAdd);
	      }
	    }
	  }
	  
	  void add(EventInfo eventInfo) {
		    synchronized (events) {
		      EventInfo found = get(key(eventInfo));
		      if (found == null) { // can't find the event in the map
		        events.put(key(eventInfo), eventInfo);
		      } else {
		        //found.update(eventInfo);
		    	  events.remove(key(eventInfo));
		    	  events.put(key(eventInfo), eventInfo);
		      }
		    }
		  }

	  //void reset(List<Event> eventsToAdd) {
	  void reset() throws IOException {
	    synchronized (events) {
	      events.clear();
	      String pageToken = null;
	        do {
	          Events eventsList = client.events().list(calendarId).setPageToken(pageToken).execute();
	          List<Event> items = eventsList.getItems();
	          for (Event eventEntry : items) {
	              add(eventEntry);
	          }

	          pageToken = eventsList.getNextPageToken();
	        } while (pageToken != null);
	    }
	  }

	  public EventInfo[] toSortedArray() {
	    synchronized (events) {
	      List<EventInfo> result = new ArrayList<EventInfo>();
	      for (EventInfo event : events.values()) {
	        result.add(event);
	      }
	      //Collections.sort(result);
	      return result.toArray(new EventInfo[0]);
	    }
	  }
}
